/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: lengleng (wangiegie@gmail.com)
 */

package com.pig4cloud.pigx.ruralLibrary.controller;

import cn.hutool.core.util.StrUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pig4cloud.pigx.common.core.util.R;
import com.pig4cloud.pigx.common.log.annotation.SysLog;
import com.pig4cloud.pigx.common.security.annotation.Inner;
import com.pig4cloud.pigx.ruralLibrary.api.entity.RecommendManageEntity;
import com.pig4cloud.pigx.ruralLibrary.api.entity.ResourceEntity;
import com.pig4cloud.pigx.ruralLibrary.api.vo.RecommendManageVo;
import com.pig4cloud.pigx.ruralLibrary.service.RecommendManageService;
import org.springframework.security.access.prepost.PreAuthorize;
import com.pig4cloud.pigx.common.excel.annotation.ResponseExcel;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import org.springframework.http.HttpHeaders;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 推荐位管理表
 *
 * @author pig
 * @date 2023-08-03 11:42:56
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/recommendManage")
@Tag(description = "recommendManage", name = "推荐位管理表管理")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class RecommendManageController {

    private final RecommendManageService recommendManageService;


    /**
     * 分页查询
     *
     * @param page              分页对象
     * @return
     */
    @Operation(summary = "分页查询", description = "分页查询")
    @GetMapping("/page")
    @PreAuthorize("@pms.hasPermission('ruralLibrary_recommend_view')")
    public R getrecommendManagePage(Page page, RecommendManageEntity recommendManage) {
        LambdaQueryWrapper<RecommendManageEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.like(StrUtil.isNotBlank(recommendManage.getResourceName()), RecommendManageEntity::getResourceName, recommendManage.getResourceName());
        wrapper.like(StrUtil.isNotBlank(recommendManage.getAuthor()), RecommendManageEntity::getAuthor, recommendManage.getAuthor());
        return R.ok(recommendManageService.page(page, wrapper));
    }


    /**
     * 通过id查询推荐位管理表
     *
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id查询", description = "通过id查询")
    @GetMapping("/{id}")
    @PreAuthorize("@pms.hasPermission('ruralLibrary_recommend_view')")
    public R getById(@PathVariable("id") Long id) {
        return R.ok(recommendManageService.getById(id));
    }

    /**
     * 新增推荐位管理表
     *
     * @param recommendManage 推荐位管理表
     * @return R
     */
    @Operation(summary = "新增推荐位管理表", description = "新增推荐位管理表")
    @SysLog("新增推荐位管理表")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('ruralLibrary_recommend_add')")
    public R save(@RequestBody RecommendManageEntity recommendManage) {

        return recommendManageService.saveEntity(recommendManage);
    }

    /**
     * 修改推荐位管理表
     *
     * @param recommendManage 推荐位管理表
     * @return R
     */
    @Operation(summary = "修改推荐位管理表", description = "修改推荐位管理表")
    @SysLog("修改推荐位管理表")
    @PutMapping
    @PreAuthorize("@pms.hasPermission('ruralLibrary_recommend_edit')")
    public R updateById(@RequestBody RecommendManageEntity recommendManage) {
        return R.ok(recommendManageService.updateById(recommendManage));
    }

    /**
     * 通过id删除推荐位管理表
     *
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除推荐位管理表", description = "通过id删除推荐位管理表")
    @SysLog("通过id删除推荐位管理表")
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('ruralLibrary_recommend_del')")
    public R removeById(@RequestBody Long[] ids) {
        return R.ok(recommendManageService.removeBatchByIds(CollUtil.toList(ids)));
    }


    /**
     * 导出excel 表格
     *
     * @param recommendManage 查询条件
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('ruralLibrary_recommend_export')")
    public List<RecommendManageEntity> export(RecommendManageEntity recommendManage) {
        return recommendManageService.list(Wrappers.query(recommendManage));
    }

    @GetMapping("/get/getByResources/{bookName}")
    public R getByResources(@PathVariable("bookName") String bookName) {
        return recommendManageService.getByResources(bookName);
    }

    @GetMapping("/get/getList/{dataTypeId}")
    @Inner(value = false)
    public R getList(@PathVariable("dataTypeId") Long dataTypeId) {
        LambdaQueryWrapper<RecommendManageEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(RecommendManageEntity::getDataTypeId,dataTypeId);
        wrapper.eq(RecommendManageEntity::getState,"1");
        return R.ok(recommendManageService.list(wrapper));
    }

    @GetMapping("/timeSequence")
    @Inner(value = false)
    public R timeSequence() {
        return recommendManageService.timeSequence();
    }
}
